package com.freemaster.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
 * 
 * @author ma
 * @date 2013-5-29 下午1:25:40
 *
 */

@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware {

	private static ApplicationContext context = null;

 

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

		context = applicationContext;
	}

	public  static Object getBean(String beanName) {

		return context.getBean(beanName);
	}

	public static Object getBean(Class c) {
		return context.getBean(c);
	}
}
